<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="700"
    destroy-on-close
    append-to-body
    @closed="emits('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      label-width="80px"
      :style="'el-dialog' === 'el-drawer' ? 'padding:0 20px' : ''"
    >
      
      <el-form-item label="所属部门" prop="sys_org_code">
        <el-input v-model="form.sys_org_code" clearable placeholder="请输入所属部门" />
      </el-form-item>

      <el-form-item label="所属公司" prop="sys_company_code">
        <el-input v-model="form.sys_company_code" clearable placeholder="请输入所属公司" />
      </el-form-item>

      <el-form-item label="流程状态" prop="bpm_status">
        <el-input v-model="form.bpm_status" clearable placeholder="请输入流程状态" />
      </el-form-item>

      <el-form-item label="类型" prop="wms_rfid_type">
        <el-input v-model="form.wms_rfid_type" clearable placeholder="请输入类型" />
      </el-form-item>

      <el-form-item label="业务编号" prop="wms_rfid_buseno">
        <el-input v-model="form.wms_rfid_buseno" clearable placeholder="请输入业务编号" />
      </el-form-item>

      <el-form-item label="业务内容" prop="wms_rfid_busecont">
        <el-input v-model="form.wms_rfid_busecont" clearable placeholder="请输入业务内容" />
      </el-form-item>

      <el-form-item label="RFID1" prop="wms_rfid_id1">
        <el-input v-model="form.wms_rfid_id1" clearable placeholder="请输入RFID1" />
      </el-form-item>

      <el-form-item label="RFID2" prop="wms_rfid_id2">
        <el-input v-model="form.wms_rfid_id2" clearable placeholder="请输入RFID2" />
      </el-form-item>

      <el-form-item label="RFID3" prop="wms_rfid_id3">
        <el-input v-model="form.wms_rfid_id3" clearable placeholder="请输入RFID3" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-switch v-model="form.status" :active-value="proxy.$CONFIG.SWITCH.ENABLE" :inactive-value="proxy.$CONFIG.SWITCH.DISABLE" />
      </el-form-item>

      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" clearable placeholder="请输入排序" />
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
  import { ref, reactive, defineExpose, onMounted,getCurrentInstance } from 'vue'
  import {useRouter} from "vue-router";
  import { ElMessage } from 'element-plus'
  import wmsRfidBuse from '@/api/apis/wms/wmsRfidBuse'
  import systemDict from '@/api/apis/system/dataDict'


  const emits = defineEmits(['success', 'closed'])
  const { proxy } = getCurrentInstance();
  const router = useRouter();

  const mode = ref('add')
  const treeList = ref([])
  const visible = ref(false)
  const isSaveing = ref(false)
  const dialogForm = ref(null)

  const titleMap = reactive({ add: '新增RFID', edit: '编辑RFID' })
  const dictData = reactive({
    platform_data_status: [],
    
  })
  const form = reactive({
    id:'',
sys_org_code:'',
sys_company_code:'',
bpm_status:'',
wms_rfid_type:'',
wms_rfid_buseno:'',
wms_rfid_busecont:'',
wms_rfid_id1:'',
wms_rfid_id2:'',
wms_rfid_id3:'',
status:proxy.$CONFIG.SWITCH.ENABLE,
sort:'0',
mer_id:'0',

  })
  const rules = reactive({
    
  })

  onMounted(async () => {
    await getDictData()
    if(router.currentRoute.value.query.open) {
    	open('add');
    }
  })

  const getDictData = () => {
    systemDict.getDict('platform_data_status').then(res => {
      dictData.platform_data_status = res.data
    })
    
  }

  const open = (type = 'add') => {
    mode.value = type
    visible.value = true
    
  }

  const submit = () => {
    dialogForm.value.validate(async (valid) => {
      if (valid) {
        isSaveing.value = true
        const res = mode.value === 'add' ? await wmsRfidBuse.save(form) : await wmsRfidBuse.update(form.id, form)
        if (res.success) {
          emits('success', form, mode.value)
          visible.value = false
          ElMessage.success(res.message)
        } else {
          ElMessage.error(res.message)
        }
        isSaveing.value = false
      }
    })
  }

  const setData = (data) => {
    for (let k in form) {
      if (data[k] || data[k] === 0) {
        form[k] = data[k]
      }
    }
  }


  defineExpose({
    open, setData
  })
</script>
